package com.jiuling.apis.controller;

import com.jiuling.apis.model.Hmodel.TBaseMerchant;
import com.jiuling.apis.model.Hmodel.TSysUser;
import com.jiuling.apis.model.Pmodel.Pmerchant;
import com.jiuling.apis.model.Pmodel.User;
import com.jiuling.apis.service.basic.HomeServiceI;
import com.jiuling.apis.service.basic.MerchantServiceI;
import com.jswzc.framework.controller.BaseController;
import com.jswzc.framework.entity.MessageBox;
import com.jswzc.framework.util.StringUtil;
import com.jswzc.model.Pager;
import com.jswzc.model.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tomchen on 17/6/7.
 */
@Controller
@RequestMapping("/merchant")
public class MerchantController extends BaseController {

    @InitBinder("pmerchant")
    public void initBinderFormBean1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("pmerchant.");
    }

    @Autowired
    private MerchantServiceI merchantService;

    @RequestMapping("toMerchant")
    public ModelAndView toMerchant(HttpServletRequest request, ModelAndView mv) throws Exception {

        String identityCode = (String) getSession().getAttribute("identityCode");
        if (!StringUtil.isNullOrEmpty(identityCode)) {
            Map<String,Object> merchantMap = merchantService.getMerchantWithStrategyById(identityCode);
            mv.addObject("merchant", merchantMap);
        }


        mv.setViewName("merchant/merchant");
        return mv;
    }

    /**
     * 查询第一级类目
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryCategoryFirst")
    public void queryCategoryFirst(HttpServletResponse response) throws Exception {
        List<Map<String, Object>> list = merchantService.queryCategoryFirst();
        writeJson(list, response);
    }

    /**
     * 查询次级类目
     *
     * @param parent_code
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryCategoryByParent")
    public void queryCategoryByParent(String parent_code, HttpServletResponse response) throws Exception {
        List<Map<String, Object>> list = merchantService.queryCategoryByParent(parent_code);
        writeJson(list, response);
    }

    @RequestMapping("doSave")
    public void doSave(HttpServletResponse response, HttpServletRequest request, Pmerchant pmerchant) {
        MessageBox mb = getBox();
        String userId = (String) getSession().getAttribute("userId");
        String identityCode = (String) getSession().getAttribute("identityCode");

        try {
            String newIdentityCode = merchantService.doSave(pmerchant,userId,identityCode);
            if (!StringUtil.isNullOrEmpty(newIdentityCode)) {
                getSession().setAttribute("identityCode", newIdentityCode);
            }
        } catch (Exception e) {
            mb.setSuccess(false);
            mb.setMsg("操作异常!");
        }
        writeJson(mb, response);

    }

    @RequestMapping("toApprove")
    public ModelAndView toHome(HttpServletRequest request, ModelAndView mv) throws Exception {

        mv.setViewName("merchant/merchantList");
        return mv;
    }

    /**
     * 展示商户列表信息
     * @param response
     * @param request
     */
    @RequestMapping("queryMerchant")
    public void queryMerchant(HttpServletResponse response, HttpServletRequest request)throws Exception{
        //必要的分页参数
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("state", request.getParameter("state"));
        Pager<Map<String,Object>> pager = new Pager();
        pager.setParameters(params);
        pager.setStartRecord(Integer.parseInt(request.getParameter("offset")));
        pager.setPageSize(Integer.parseInt(request.getParameter("limit")));

        pager=merchantService.queryMerchant(pager);

        Paging<Map<String,Object>> paging = new Paging<>();
        paging.setRows(pager.getExhibitDatas());
        paging.setTotal((long) pager.getRecordCount());

        writeJson(paging,response);
    }

    @RequestMapping("toMerchantDetail")
    public ModelAndView toMerchantDetail(HttpServletRequest request,String id, ModelAndView mv) throws Exception {

        Map<String,Object> merchantMap = merchantService.getMerchantWithStrategyById(id);
        mv.addObject("merchant", merchantMap);

        mv.setViewName("merchant/merchantDetail");
        return mv;
    }

    @RequestMapping("doApprove")
    public void doApprove(HttpServletResponse response, HttpServletRequest request, Pmerchant pmerchant) {
        MessageBox mb = getBox();
        try {
            merchantService.doApprove(pmerchant);
        } catch (RuntimeException e) {
            mb.setSuccess(false);
            mb.setMsg(e.getMessage());
        } catch (Exception e) {
            mb.setSuccess(false);
            mb.setMsg("操作异常!");
        }
        writeJson(mb, response);

    }

}
